async function fetchAndDisplayIPs() {
    try {
        const response = await fetch('/api/get_latest_ips');
        const data = await response.json();
        
        if (response.ok) {
            // 显示IPv4地址
            const ipv4List = document.getElementById('ipv4-list');
            ipv4List.innerHTML = '';
            data.ipv4.forEach(ip => {
                const li = document.createElement('li');
                li.className = 'p-2 hover:bg-white/10 rounded transition-colors duration-200 cursor-pointer';
                li.textContent = ip;
                li.onclick = () => {
                    navigator.clipboard.writeText(ip);
                    showToast('IP已复制到剪贴板');
                };
                ipv4List.appendChild(li);
            });
            
            // 显示IPv6地址
            const ipv6List = document.getElementById('ipv6-list');
            ipv6List.innerHTML = '';
            data.ipv6.forEach(ip => {
                const li = document.createElement('li');
                li.className = 'p-2 hover:bg-white/10 rounded transition-colors duration-200 cursor-pointer';
                li.textContent = ip;
                li.onclick = () => {
                    navigator.clipboard.writeText(ip);
                    showToast('IP已复制到剪贴板');
                };
                ipv6List.appendChild(li);
            });
            
            // 更新修改时间
            document.getElementById('modification-date').textContent = 
                `最后更新: ${data.timestamp}`;
        }
    } catch (error) {
        console.error('获取IP地址失败:', error);
    }
}

function showToast(message) {
    const toast = document.createElement('div');
    toast.className = 'fixed bottom-4 right-4 bg-black/70 text-white px-4 py-2 rounded-lg backdrop-blur-sm';
    toast.textContent = message;
    document.body.appendChild(toast);
    
    setTimeout(() => {
        toast.remove();
    }, 2000);
}

// 页面加载时获取IP
document.addEventListener('DOMContentLoaded', fetchAndDisplayIPs);

// 每5分钟刷新一次
setInterval(fetchAndDisplayIPs, 5 * 60 * 1000); 